import 'package:amway_superapp_creator_plugin/models/module_list.dart';
import 'package:amway_superapp_creator_plugin/utils/log_util.dart';
import 'package:amway_superapp_creator_plugin/utils/sp_util.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:flutter/cupertino.dart';

import '../api.dart';
import '../view_state.dart';

class ModulePageVM extends BaseModel {
  ModulePageVM({@required Api api}) {
    this.api = api;
    // processTestData();
  }

  // List<ModuleItem> moduleItems = MockData.generateModuleItem();
  final List<ModuleItem> moduleItems = new List();

  ModuleData moduleData;

  bool _isEdited=false;

  bool get isEdited => _isEdited;

  set isEdited(bool value) {
    _isEdited = value;
  }

  bool _isLoading=false;

  var _ada;
  var _amwayId;

  void setData(var ada, var amwayId) {
    this._ada=ada;
    this._amwayId=amwayId;
    setState(ViewState.Busy);
    api.getModuleList((json) {
      try {
        setState(ViewState.Idle);
        moduleData = ModuleData.fromJson(json);
        moduleData.moduleItemList.forEach((element) {
          switch(element.moduleName){
            case "NONE":
              element.actionState=true;
              break;
            case "PERSONAL":
              element.actionState=true;
              break;
            case "COMBINATION":
              element.actionState=true;
              break;
            case "RECOMMEND":
              element.actionState=true;
              break;
            case "CIRCLE":
              element.actionState=true;
              SpUtil.setData(
                  "${SpUtil.official_circle}$ada", element.showOfficialFlag);
              break;
            case "HEALTH_FOOD":
              element.actionState=true;
              break;
            case "LIVE_VIDEO":
              element.actionState=false;
              break;
            case "ACTIVITY":
              element.actionState=false;
              break;
          }
        });

        moduleItems.addAll(moduleData.moduleItemList);
        notifyListeners();
      } on Exception catch (e) {
        // 任意一个异常
        print('Unknown exception: $e');
      } catch (e) {
        // 非具体类型
        print('Something really unknown: $e');
      }
    }, (data) {
      setState(ViewState.Idle);
      LogUtil.e(data.toString());
    }, ada);
  }

  void updateModuleList() {

    if(!_isLoading){
      _isLoading=true;
    }else{
      ToastUtils.showShortToast('请稍候');
      return;
    }

    setState(ViewState.Busy);
    moduleData.moduleItemList.clear();
    moduleData.moduleItemList.addAll(moduleItems);
    api.updateModuleList(moduleData, (json) {
      _isLoading=false;
      isEdited=false;
      setState(ViewState.Idle);
      ToastUtils.showShortToast("保存成功");
    }, (json) {
      _isLoading=false;
      setState(ViewState.Idle);
      ToastUtils.showShortToast("保存失败");
    });
  }

  void changeSequence(int from, int to) {
    _isEdited=true;
    ModuleItem last = moduleItems.elementAt(from);
    moduleItems.removeAt(from);
    last.no = to;
    moduleItems.insert(to, last);
    moduleItems.elementAt(from).no = from;
    notifyListeners();
  }

  void onItemCheckTap(String moduleName) {
    _isEdited=true;
    Key key = ValueKey(moduleName);

    int listIndex = moduleItems.indexWhere((ModuleItem d) => d.key == key);

    bool shouldShowToast=false;
    int checkSum=0;
    moduleItems.forEach((element) {
      if(element.state=="ON"){
        checkSum++;
      }
    });

    if(checkSum==1&&moduleItems[listIndex].state=="ON"){
      ToastUtils.showShortToast('至少选择1个模块哦');
    }else{
      moduleItems.elementAt(listIndex).state =
      moduleItems.elementAt(listIndex).state == "ON" ? "OFF" : "ON";
      notifyListeners();
    }
  }

  void updateModuleState() {
    moduleItems.clear();
    if(_ada!=null&&_amwayId!=null){
      setData(_ada, _amwayId);
    }
  }
}
